package com.zl.learning.io.bio;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static class Server1 {
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(888);
                System.out.println("server start at 888");
                Socket socket = serverSocket.accept();
                InputStream in = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len = in.read(bytes);
                System.out.println(new String(bytes, 0, len));
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Client1 {
        public static void main(String[] args) {
            try {
                Socket socket = new Socket();
                socket.bind(new InetSocketAddress(887));
                socket.connect(new InetSocketAddress("localhost", 888));
                OutputStream out = socket.getOutputStream();
                out.write("Hello".getBytes());
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void  test1(){
        String val = "";
    }
}
